<?php
defined('IN_IA') or exit('Access Denied');

$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if($op == 'det'){
		$uniacid = $_GPC['uniacid'];

		$log = pdo_fetchall("SELECT orderno,FROM_UNIXTIME(ptime,'%Y-%m-%d %H:%i') as ptime,cash,discount,refund_cash FROM ".tablename('tg_distributor_cash')." WHERE parent_uniacid = :parent_uniacid and uniacid =:uniacid  order by ptime desc ", array(':parent_uniacid' => $_W['uniacid'],':uniacid' => $uniacid));
		foreach($log as &$vs)
		{
			$orderno = pdo_fetch("SELECT functionid FROM ".tablename('tg_function_order')." WHERE orderno = :orderno  ", array(':orderno' => $vs['orderno']));
			$ordername = pdo_fetch("SELECT name,type FROM ".tablename('tg_function')." WHERE id = :functionid  ", array(':functionid' => $orderno['functionid']));

			$vs['discount'] = $vs['discount']*100;
			if($ordername['type']=="3"){
				$vs['name'] = "短信购买";
			}else if($ordername['type']=="4"){
				$vs['name'] = "订单购买";
			}else{
				$vs['name'] = $ordername['name'];
			}
			
		}
		
		die(json_encode($log));
	}
if($op == 'sendstatus'){
		die(json_encode($result));
}
if($op == 'addstatus'){
		$uid = $_GPC['uid'];
		$note = $_GPC['note'];
		$data['uid'] = $uid;
		$data['selluid'] = $_W['uid'];
		$data['note'] = $note;
		$data['addtime']= TIMESTAMP;
		
		$storage = pdo_insert('tg_agent_users_note', $data);
		
		if(empty($storage))
		{
			$result['message']="添加失败";
		}else{
			$result['message']="添加成功";
		}
		die(json_encode($result));
	}
if($op == 'status-list'){
		$uid = $_GPC['uid'];
		
		$status_list = pdo_fetchall("SELECT id,FROM_UNIXTIME(addtime,'%Y-%m-%d %H:%i') as addtime,note,selluid FROM ".tablename('tg_agent_users_note')." WHERE uid = :uid order by addtime desc ", array(':uid' => $uid));
	foreach($status_list as &$list)
	{
		$name = pdo_fetch("select username from".tablename('users')."where uid = :uid ",array(':uid' => $list['selluid']));
		$list['uname']=$name['username'];
	}
		die(json_encode($status_list));
	}
if($op =='detstatus'){
	$id = $_GPC['id'];	
	$lists = pdo_fetch("select id,FROM_UNIXTIME(addtime,'%Y-%m-%d %H:%i') as addtime,note,selluid from".tablename('tg_agent_users_note')."where id = :id order by addtime desc ",array(':id' => $id));
	$uid = pdo_fetch("select uid from".tablename('tg_agent_users_note')."where id = :id ",array(':id' => $id));
	$name = pdo_fetch("select username from".tablename('users')."where uid = :uid ",array(':uid' => $uid['uid']));
	$lists['name'] = $name['username'];	
	$uname = pdo_fetch("select username from".tablename('users')."where uid = :uid ",array(':uid' => $lists['selluid']));
	$lists['uname'] = $uname['username'];
	die(json_encode($lists));
}
if($op =='update_status'){
	$id = $_GPC['id'];
	$note = $_GPC['note'];
	
	$updete = pdo_update("tg_agent_users_note",array('note'=>$note,'selluid'=>$_W['uid']),array('id'=>$id));
	
	$uid = pdo_fetch("select uid from".tablename('tg_agent_users_note')."where id = :id ",array(':id' => $id));
	$status_list = pdo_fetchall("SELECT id,FROM_UNIXTIME(addtime,'%Y-%m-%d %H:%i') as addtime,note,selluid FROM ".tablename('tg_agent_users_note')." WHERE uid = :uid order by addtime desc ", array(':uid' => $uid['uid']));
	foreach($status_list as &$list)
	{
		$name = pdo_fetch("select username from".tablename('users')."where uid = :uid ",array(':uid' => $list['selluid']));
		$list['uname']=$name['username'];
	}
	$result['list'] = $status_list;
	
	$name = pdo_fetch("select username from".tablename('users')."where uid = :uid ",array(':uid' => $uid['uid']));
	$result['name'] = $name['username'];
	if(empty($updete))
		{
			
			$result['message']="修改成功";
		}else{
			$result['message']="修改失败";
		}
	die(json_encode($result));
}
if($op =='detail_status'){
	$id = $_GPC['id'];
	
	$uid = pdo_fetch("select uid from".tablename('tg_agent_users_note')."where id = :id ",array(':id' => $id));
	$status_list = pdo_fetchall("SELECT id,FROM_UNIXTIME(addtime,'%Y-%m-%d %H:%i') as addtime,note,selluid FROM ".tablename('tg_agent_users_note')." WHERE uid = :uid order by addtime desc ", array(':uid' => $uid['uid']));
	foreach($status_list as &$list)
	{
		$name = pdo_fetch("select username from".tablename('users')."where uid = :uid ",array(':uid' => $list['selluid']));
		$list['uname']=$name['username'];
	}
	$result['list'] = $status_list;
	
	$name = pdo_fetch("select username from".tablename('users')."where uid = :uid ",array(':uid' => $uid['uid']));
	$result['name'] = $name['username'];
	if(empty($updete))
		{
			
			$result['message']="撤销成功";
		}else{
			$result['message']="撤销失败";
		}
	die(json_encode($result));
}
if($op =='delete_status'){
	$id = $_GPC['id'];
	$uid = pdo_fetch("select uid from".tablename('tg_agent_users_note')."where id = :id ",array(':id' => $id));
	
	$delete=pdo_query("delete from " . tablename('tg_agent_users_note') . " where id= '{$id}'");

	$status_list = pdo_fetchall("SELECT id,FROM_UNIXTIME(addtime,'%Y-%m-%d %H:%i') as addtime,note,selluid FROM ".tablename('tg_agent_users_note')." WHERE uid = :uid order by addtime desc ", array(':uid' => $uid['uid']));
	foreach($status_list as &$list)
	{
		$name = pdo_fetch("select username from".tablename('users')."where uid = :uid ",array(':uid' => $list['selluid']));
		$list['uname']=$name['username'];
	}
	$result['list'] = $status_list;
	$name = pdo_fetch("select username from".tablename('users')."where uid = :uid ",array(':uid' => $uid['uid']));
	$result['name'] = $name['username'];
	if(empty($delete))
		{
			$result['message']="修改成功";
		}else{
			$result['message']="修改失败";
		}
	die(json_encode($result));
}
if($op == 'out'){
		$uid = $_GPC['uid'];
		$isout=pdo_update("users",array('isout'=>-1),array('uid'=>$uid));
		die(json_encode($isout));
}
if($op == 'reout'){
		$uid = $_GPC['uid'];
		$isout=pdo_update("users",array('isout'=>0),array('uid'=>$uid));
		die(json_encode($isout));
}
if($op == 'update_referral'){
		$uid = $_GPC['uid'];
		$referral = $_GPC['referral'];
		
		$updete = pdo_update("users",array('referral'=>$referral),array('uid'=>$uid));
		echo "<script>location.href='".web_url('agent/agent_users',array('page' =>$_GPC['page']))."';</script>";
		exit();
		
}
if ($op == 'display') {
	$status = intval($_GPC['status']);

	if ($status != 5) {
		$condition = "and isout = {$status}";
	}


	$referral = pdo_fetch("select referral from" . tablename('account_wechats') . "where uniacid = :uniacid ",array(':uniacid' => $_W['uniacid']));

	$pindex = max(1, intval($_GPC['page']));
	$psize = 10;
	

	if (!empty($_GPC['keyword_username'])) {
		$condition .= " AND username LIKE '%{$_GPC['keyword_username']}%' ";
	}
	if (!empty($_GPC['keyword_gzname'])) {
		$condition .= " AND uid in (select uid from " . tablename('uni_account_users') . " where uniacid in (select uniacid from " . tablename('account_wechats') . " where name LIKE '%{$_GPC['keyword_gzname']}%')) ";
	}
	if (!empty($_GPC['keyword_mobile'])) {
		$condition .= " AND uid in (select uid from " . tablename('users_profile') . " where mobile LIKE '%{$_GPC['keyword_mobile']}%') ";
	}
	
	$alluser = pdo_fetchall("select uid,username,FROM_UNIXTIME(joindate,'%Y-%m-%d %H:%i') as joindate from" . tablename('users') . " where referral = :referral " . $condition . " order by uid desc " . "LIMIT " . ($pindex - 1) * $psize . ',' . $psize,array(':referral' => $referral['referral']));
	foreach($alluser as &$user)
	{
		$detail = pdo_fetch("SELECT realname,mobile,qq FROM ".tablename('users_profile')." WHERE uid = :uid  ", array(':uid' => $user['uid']));
		
		$user['realname']=$detail['realname'];
		$user['mobile']=$detail['mobile'];
		$user['qq']=$detail['qq'];
		
		$uniacid = pdo_fetch("SELECT uniacid FROM ".tablename('uni_account_users')." WHERE uid = :uid  ", array(':uid' => $user['uid']));
		if (empty($uniacid['uniacid'])) {
			$user['gzname']="未接入公众号";
		}else{
			$gzname = pdo_fetch("SELECT name,ordernum,endtime FROM ".tablename('account_wechats')." WHERE uniacid =:uniacid  ", array(':uniacid' => $uniacid['uniacid']));
			$user['gzname']=$gzname['name'];
		
		
			$log = pdo_fetchall("SELECT FROM_UNIXTIME(ptime,'%Y-%m-%d %H:%i') as ptime,cash,refund_cash FROM ".tablename('tg_distributor_cash')." WHERE parent_uniacid = :parent_uniacid and uniacid =:uniacid  ", array(':parent_uniacid' => $_W['uniacid'],':uniacid' => $uniacid['uniacid']));
			foreach($log as &$vs)
			{
				$user['sum_cash'] += $vs['cash'];
				$user['sum_refund_cash'] += $vs['refund_cash'];
			}
			
			$total = pdo_fetchcolumn("SELECT COUNT(*) FROM ".tablename('tg_distributor_cash')."WHERE parent_uniacid = :parent_uniacid and uniacid =:uniacid  ", array(':parent_uniacid' => $_W['uniacid'],':uniacid' => $uniacid['uniacid']));
			$lasttime = pdo_fetch("SELECT FROM_UNIXTIME(ptime,'%Y-%m-%d %H:%i') as ptime FROM ".tablename('tg_distributor_cash')." WHERE parent_uniacid = :parent_uniacid and uniacid =:uniacid  order by ptime desc", array(':parent_uniacid' => $_W['uniacid'],':uniacid' => $uniacid['uniacid']));
			
			$user['total']=$total;
			$user['lasttime']=$lasttime['ptime'];
			$user['uniacid']=$uniacid['uniacid'];
			$user['ordernum']=$gzname['ordernum'];
			$user['endtime']='--';
			if(!empty($gzname['endtime']))
			{
				$user['endtime']=date('Y-m-d',$gzname['endtime']);
			}

		}
	}
	$alluser2 = pdo_fetchall("select uid from" . tablename('users') . "where referral = :referral " . $condition . " order by uid desc ",array(':referral' => $referral['referral']));

	$total = count($alluser2);
	$pager = pagination($total, $pindex, $psize);
	
	include wl_template('agent/agent_users');
}
exit();
	
